/*
 * jlb
 */
package com.jlb.jlb.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.jlb.core.base.BaseDAO;
import com.jlb.core.base.BaseSVImpl;
import com.jlb.core.entity.Page;
import com.jlb.core.exceptions.BaseException;
import com.jlb.core.exceptions.JlbOreLogException;
import com.jlb.core.tools.StringTools;
import com.jlb.jlb.dao.JlbOreLogDAO;
import com.jlb.jlb.entity.JlbOreLog;
import com.jlb.jlb.entity.JlbOreLogType;
import com.jlb.jlb.entity.TransferType;
import com.jlb.user.dao.UserDAO;
import com.jlb.user.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("jlbOreLogSV")
@Slf4j
public class JlbOreLogSVImpl extends BaseSVImpl<JlbOreLog, Long> implements JlbOreLogSV {

    @Autowired
    private JlbOreLogDAO jlbOreLogDAO;

    @Autowired
    private UserDAO userDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return jlbOreLogDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(JlbOreLog entity) throws BaseException {
        entity.setCreateTime(new Date());
        super.save(entity);
    }

    /**
     * 加载一个对象JlbOreLog
     *
     * @param id * @param outUserCode 转出账户编码
     * @return JlbOreLog
     */
    @Override
    public JlbOreLog load(Long id, String outUserCode) {
        if (id == null && outUserCode == null) {
            throw new JlbOreLogException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("outUserCode", outUserCode);
        return jlbOreLogDAO.load(param);
    }

    /**
     * 加载一个对象JlbOreLog 通过id
     *
     * @param id
     * @return JlbOreLog
     */
    @Override
    public JlbOreLog loadById(Long id) {
        if (id == null) {
            throw new JlbOreLogException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return jlbOreLogDAO.loadById(id);
    }

    /**
     * 加载一个对象JlbOreLog 通过outUserCode
     *
     * @param outUserCode 转出账户编码
     * @return JlbOreLog
     */
    @Override
    public JlbOreLog loadByOutUserCode(String outUserCode) {
        if (outUserCode == null) {
            throw new JlbOreLogException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return jlbOreLogDAO.loadByOutUserCode(outUserCode);
    }


    /**
     * 根据主键id,oldStates 共同更新 JlbOreLog 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(Long id, JlbOreLogType newState, JlbOreLogType... oldStates) {
        if (id == null) {
            throw new JlbOreLogException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new JlbOreLogException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new JlbOreLogException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        jlbOreLogDAO.updateStateById(id, new Date(), newState, oldStates);
    }

    /**
     * 根据主键outUserCode,oldStates 共同更新 JlbOreLog 的状态到newState状态
     *
     * @param outUserCode 转出账户编码
     * @param newState    新状态
     * @param oldStates   旧状态集合
     */
    @Override
    public void updateStateByOutUserCode(String outUserCode, JlbOreLogType newState, JlbOreLogType... oldStates) {
        if (outUserCode == null) {
            throw new JlbOreLogException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new JlbOreLogException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new JlbOreLogException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        jlbOreLogDAO.updateStateByOutUserCode(outUserCode, new Date(), newState, oldStates);
    }

    /**
     * 根据主键id 更新 JlbOreLog 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(Long id, JlbOreLogType state) {
        if (id == null) {
            throw new JlbOreLogException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        jlbOreLogDAO.updateById(id, state, new Date());
    }

    /**
     * 根据主键outUserCode 更新 JlbOreLog 的状态到另一个状态
     *
     * @param outUserCode 转出账户编码
     * @param state       状态
     */
    @Override
    public void updateByOutUserCode(String outUserCode, JlbOreLogType state) {
        if (outUserCode == null) {
            throw new JlbOreLogException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        jlbOreLogDAO.updateByOutUserCode(outUserCode, state, new Date());
    }


    /**
     * 删除对象JlbOreLog
     *
     * @param id * @param outUserCode 转出账户编码
     * @return JlbOreLog
     */
    @Override
    public void delete(Long id, String outUserCode) {
        if (id == null && outUserCode == null) {
            throw new JlbOreLogException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("outUserCode", outUserCode);
        jlbOreLogDAO.delete(param);
    }


    /**
     * 查询JlbOreLog分页
     *
     * @param jlbOreLog 对象
     * @param offset    查询开始行
     * @param limit     查询行数
     * @return List<JlbOreLog>
     */
    @Override
    public List<JlbOreLog> list(JlbOreLog jlbOreLog, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (jlbOreLog != null) {
            map = JSON.parseObject(JSON.toJSONString(jlbOreLog, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return jlbOreLogDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(JlbOreLog jlbOreLog) {
        Map<String, Object> map = null;
        if (jlbOreLog != null) {
            map = JSON.parseObject(JSON.toJSONString(jlbOreLog, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return jlbOreLogDAO.count(map);
    }

    /**
     * 查询JlbOreLog分页
     *
     * @param id     * @param outUserCode  转出账户编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<JlbOreLog>
     */
    @Override
    public List<JlbOreLog> list(Long id, String outUserCode, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("outUserCode", outUserCode);
        return jlbOreLogDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id, String outUserCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("outUserCode", outUserCode);
        return jlbOreLogDAO.count(param);
    }

    /**
     * 查询JlbOreLog分页
     *
     * @param userCode    用户编码
     * @param type        转账类型
     * @param offset      查询开始行
     * @param limit       查询行数
     * @param sortColumns
     * @return List<JlbOreLog>
     */
    @Override
    public List<JlbOreLog> listForUser(String userCode, TransferType type, int offset, int limit, String sortColumns) {
        if (StringTools.isEmpty(userCode)) {
            throw new JlbOreLogException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        if (type == TransferType.In) {
            param.put("inUserCode", userCode);
        } else if (type == TransferType.Out) {
            param.put("outUserCode", userCode);
        } else {
            param.put("inUserCode", userCode);
            param.put("outUserCode", userCode);
        }
        param.put("sortColumns", sortColumns);
         List<JlbOreLog> jlbOreLogs = jlbOreLogDAO.listForUser(param, new RowBounds(offset, limit));
         User inUser;
         User outUser;
        for (JlbOreLog jlbOreLog: jlbOreLogs) {
            inUser = userDAO.loadByCodeNotRelation(jlbOreLog.getInUserCode());
            outUser = userDAO.loadByCodeNotRelation(jlbOreLog.getOutUserCode());
            jlbOreLog.setInAccount(inUser != null ? inUser.getAccount() : null);
            jlbOreLog.setOutAccount(outUser != null ? outUser.getAccount() : null);
        }
         return jlbOreLogs;
    }

}
